﻿#include "PyFITKMaterialsFE.h"
#include "FITKAbaMaterial.h"
#include "FITKAbaMaterialElastic.h"
#include "FITKAbaMaterialDensity.h"
#include "FITKAbaMaterialExpansion.h"
#include "FITKAbaMaterialDamping.h"

namespace Acoustics
{
    PyFITKMaterialsFE::PyFITKMaterialsFE(const QString & name)
    {
    //获取材料
        if ( name == "") return;
        _FEMaterialName = name;
    }

    bool PyFITKMaterialsFE::setField(int type, double value)
    {
        if (!_mgr || !_FEMaterial || type < 1 || type>7 || value < 0)return false;

        if (type == 2)  return true;
        if (type == 1 || type == 3) {
            _FEMaterial->addMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Elastic);
            Interface::FITKAbaMaterialElastic* elasticBehavior = dynamic_cast<Interface::FITKAbaMaterialElastic*>(
                _FEMaterial->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Elastic));
            if (elasticBehavior) {
                elasticBehavior->setMaterialElasticType(Interface::FITKAbaMaterialBehaviorEnum::IsoTropic);
                if (type == 1) elasticBehavior->setIsotropicYoungsModulus(value);
                if (type == 3) elasticBehavior->setIsotropicPoissonRatio(value);
                return  true;
            }
        }
        if (type == 4) {
            _FEMaterial->addMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Density);
            Interface::FITKAbaMaterialDensity* densityBehavior = dynamic_cast<Interface::FITKAbaMaterialDensity*>(
                _FEMaterial->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Density));
            if (densityBehavior) {
                densityBehavior->setMassDensity(value);
                return true;
            }
        }
        if (type == 5 || type == 6) {
            _FEMaterial->addMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Expansion);
            Interface::FITKAbaMaterialExpansion* expansionBehavior = dynamic_cast<Interface::FITKAbaMaterialExpansion*>(
                _FEMaterial->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Expansion));
            if (expansionBehavior) {
                expansionBehavior->setExpansionType(Interface::FITKAbaMaterialBehaviorEnum::Isotropic);
                if (type == 5) expansionBehavior->setIsoTropicExpansionCoeff(value);
                if (type == 6) expansionBehavior->setIsotropicTemperature(value);
                return true;
            }
        }
        if (type == 7) {
            _FEMaterial->addMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Damping);
            Interface::FITKAbaMaterialDamping* dampingBehavior = dynamic_cast<Interface::FITKAbaMaterialDamping*>(
                _FEMaterial->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::Damping));
            if (dampingBehavior) {
                dampingBehavior->setStructural(value);
                return true;
            }
        }
        return false;
    }

    QString PyFITKMaterialsFE::getName()
    {
        if (_FEMaterial)
            return _FEMaterial->getDataObjectName();
        return QString();
    }

    bool PyFITKMaterialsFE::setName(const QString & name)
    {
        //空字符直接返回
        if (name == "" || !_FEMaterial || !_mgr)return false;

        //获取名称-查重处理
        if (_mgr->getDataByName(name) && _FEMaterial->getDataObjectName() != name)return false;

        _FEMaterial->setDataObjectName(name);
        return true;
    }

    bool PyFITKMaterialsFE::createMaterial(const QString & name)
    {
        if (!_mgr || name == "")return false;
        if (_mgr->getDataByName(name))return false;

        _FEMaterial = new Interface::FITKAbaMaterial();
        _FEMaterial->setDataObjectName(name);
        _mgr->appendDataObj(_FEMaterial);
        return true;
    }

    void PyFITKMaterialsFE::setMaterialManager(Interface::FITKMaterialManager * mgr)
    {
        _mgr = mgr;
        if(!_mgr|| !_mgr->getDataByName(_FEMaterialName))return;
        _FEMaterial = dynamic_cast<Interface::FITKAbaMaterial*>(_mgr->getDataByName(_FEMaterialName));
    }
}